package com.jian.java.controller;

import com.jian.java.entities.Dept;
import com.jian.java.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jiange
 * @create 2021-05-30-17:36
 */
@RestController
@Slf4j
public class ConsumerController {
    @Autowired
    private DeptService deptService;

    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/all")
    public List<Dept> getAll(){
//        return restTemplate.getForObject("http://DEPT/dept/getall",List.class);
        return deptService.getAll();
    }

    @GetMapping("/get/{id}")
    public Dept get(@PathVariable int id){
//        return restTemplate.getForObject("http://DEPT/dept/get/"+id,Dept.class);
        return deptService.get(id);
    }

    @GetMapping("/message")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        services.forEach(System.out::println);//输出的是application的名字  负载均衡写服务名的时候也要写这个名字
        List<ServiceInstance> dept = discoveryClient.getInstances("DEPT");
        for (ServiceInstance instance : dept) {
            log.info(instance.getServiceId()+"\t"+instance.getUri());
        }
        return null;
    }
}
